package com.wzapp.sitposture.authentication.repository;

import com.wzapp.sitposture.authentication.pojo.entity.MobileAndCode;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

/**
 * 功能: 手机号和验证码Repository. <br/>
 *
 * @author ql
 * @date 2018-02-06 14:29
 * @version
 * @since JDK 1.8
 */
@RepositoryRestResource(path = "mobileAndCode",exported = false)
public interface MobileAndCodeRepository extends JpaRepository<MobileAndCode,String> {

    MobileAndCode findTop1ByMobileAndDeletedOrderByUpdateTimeDesc(String mobile,Boolean deleted);

    /**
     * 验证成功后清除今天之前的记录
     * @param mobile
     */
    @Modifying
    @Query(nativeQuery = true,value = "DELETE from mobile_and_code where mobile = ?1 and date(update_time) < CURRENT_DATE")
    void deleteAllByMobile(String mobile);

    /**
     * 验证成功后更新今天的记录删除状态
     * @param mobile
     */
    @Modifying
    @Query(nativeQuery = true,value = "update mobile_and_code set deleted = TRUE where mobile = ?1 and deleted = FALSE and date(update_time) = CURRENT_DATE")
    void updateAllByMobile(String mobile);

    int countByMobileAndUpdateTimeBetween(String mobile, Long startTime,Long endTime);
}
